import { Role } from "../../../../role/Role";
import { RoleViewComp } from "../../../../role/view/RoleViewComp";
import { BattleDamageType, BattleRoleState } from "../../../scene/model/BattleEnum";
import { RoleBuff } from "../../bll/utils/RoleBuff";
import { EffectDamage } from "../../model/report/EffectDamage";
import { ISkillReport } from "../../model/report/ISkillReport";
import { RoleEffect } from "../utils/RoleEffect";

export class SkillAnimatorHitEffectDamage {
    static play(isr: ISkillReport, target: Role, effect: EffectDamage, onComplete: Function) {
        if (effect.hitEffect == BattleDamageType.Normal || effect.hitEffect == BattleDamageType.Critical || effect.hitEffect == BattleDamageType.Block) {
            setTimeout(() => {
                onComplete("damge");
            }, 300);
        } else {
            onComplete("damge");
        }
        var rva = target.RoleView;
        if (!rva) {
            return
        }
        this.bloodAni(isr, target, effect);

        if (effect.hitEffect == BattleDamageType.Normal || effect.hitEffect == BattleDamageType.Critical || effect.hitEffect == BattleDamageType.Block) {
            this.onHitAni(isr, target, effect, onComplete);

            var path = isr?.skill?.SkillModel?.effectHitPath;
            if (path) {
                isr.skill.showEffectHit(target, path);
            }
        }
    }
    //流血飘文字
    private static bloodAni(isr: ISkillReport, target: Role, effect: EffectDamage) {
        isr.skill.showEffectBlood(target, effect);
    }
    //受击动画
    private static onHitAni(isr: ISkillReport, target: Role, effect: EffectDamage, onComplete: Function) {
        var rva = target.RoleView;
        // 死亡动作
        if (effect.state == BattleRoleState.Die) {
            this.onDie(target);
        }
        else if (isr.caster) {
            rva?.animator?.damage();
        }
    }

    private static onDie(target: Role) {
        if (target.RoleView) {
            RoleEffect.die(target.RoleView.node.parent!);
            target.remove(RoleViewComp);
            RoleBuff.removeAllBuff(target);
        }
    }}